﻿// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
//
// LocationsPage.xaml.h
// Declaration of the LocationsPage class
//

#pragma once

#include "LocationsPage.g.h"
#include "Common\NavigationHelper.h"
#include "Common\BooleanToVisibilityConverter.h" // Required by generated header

namespace Weathr
{
    // Defines an item that appears on the Manage Locations page.
    [Windows::UI::Xaml::Data::Bindable]
    [Windows::Foundation::Metadata::WebHostHidden]
    public ref class LocationItem sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged
    {
    internal:
        LocationItem(Forecast^ forecast) : m_forecast(forecast)
        {
            Title = forecast->Locality;
            Subtitle = forecast->CountryRegion;
        }

    public:
        virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged;

        property Forecast^ Item
        {
            Forecast^ get() { return m_forecast; }
        }

        property Platform::String^ Title;
        property Platform::String^ Subtitle;

    private:
        Forecast^ m_forecast;

        inline void OnPropertyChanged(Platform::String^ propertyName)
        {
            PropertyChanged(this, ref new Windows::UI::Xaml::Data::PropertyChangedEventArgs(propertyName));
        }
    };

    // Groups locations alphabetically. Each item in the group shares the same initial letter.
    [Windows::UI::Xaml::Data::Bindable]
    [Windows::Foundation::Metadata::WebHostHidden]
    public ref class LocationGroup sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged
    {
    internal:
        LocationGroup(Platform::String^ title) : m_items(ref new Platform::Collections::Vector<LocationItem^>())
        {
            Title = title;
        }

    public:
        virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged;

        property Windows::Foundation::Collections::IObservableVector<LocationItem^>^ Items
        {
            Windows::Foundation::Collections::IObservableVector<LocationItem^>^ get() { return m_items; }
        }
        property Platform::String^ Title;

    private:
        Platform::Collections::Vector<LocationItem^>^ m_items;

        inline void OnPropertyChanged(Platform::String^ propertyName)
        {
            PropertyChanged(this, ref new Windows::UI::Xaml::Data::PropertyChangedEventArgs(propertyName));
        }
    };

    /// <summary>
    /// A page that displays a collection of item previews.  In the Split Application this page
    /// is used to display and select one of the available groups.
    /// </summary>
    [Windows::Foundation::Metadata::WebHostHidden]
    public ref class LocationsPage sealed
    {
    public:
        LocationsPage();

        /// <summary>
        /// This can be changed to a strongly typed view model.
        /// </summary>
        property Windows::Foundation::Collections::IObservableMap<Platform::String^, Platform::Object^>^ DefaultViewModel
        {
            Windows::Foundation::Collections::IObservableMap<Platform::String^, Platform::Object^>^  get();
        }

        /// <summary>
        /// NavigationHelper is used on each page to aid in navigation and 
        /// process lifetime management
        /// </summary>
        property Common::NavigationHelper^ NavigationHelper
        {
            Common::NavigationHelper^ get();
        }

    protected:
        virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;
        virtual void OnNavigatedFrom(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;

    private:
        void LoadState(Platform::Object^ sender, Common::LoadStateEventArgs^ e);
        void SaveState(Platform::Object^ sender, Common::SaveStateEventArgs^ e);

        static Windows::UI::Xaml::DependencyProperty^ _defaultViewModelProperty;
        static Windows::UI::Xaml::DependencyProperty^ _navigationHelperProperty;

        void OnForecastRemoved(Forecast ^forecast);
        void ClearSelection_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
        void Reset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
        void DeleteSelection_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
        void semanticZoom_ViewChangeStarted(Platform::Object^ sender, Windows::UI::Xaml::Controls::SemanticZoomViewChangedEventArgs^ e);
        void itemGridView_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e);
        void Image_ImageFailed(Platform::Object^ sender, Windows::UI::Xaml::ExceptionRoutedEventArgs^ e);
    };
}
